const loginAction = /\/login/gi;
export default {
    namespace: 'payoffSharedModel',
    state: {
        sharePeriod: null,
        shareOrganizationCode: null
    },
    reducers: {
        setShared(state, action) {
            return {
                ...state,
                ...action.payload
            };
        },
        clear(state) {
            return {
                ...state,
                sharePeriod: null,
                shareOrganizationCode: null
            };
        }
    },
    effects: {
        *sharedState(state, { put }) {
            yield put({
                type: 'setShared',
                payload: state.payload
            });
            // eslint-disable-next-line no-unused-expressions
            state.callback && state.callback();
        }
    },
    subscriptions: {
        setup(props) {
            const { dispatch, history } = props;
            return history.listen(({ pathname }) => {
                if (String(pathname).search(loginAction) > -1) { // 如果是登录登出操作清空缓存
                    dispatch({
                        type: 'clear'
                    });
                }
            });
        }
    }
};
